/*

This file is part of Rooms3D.

Copyright (C) 2008 Pixecur Technologies Incorporated (Esotera).
Visit Esotera at http://www.esoteras3.com
 
Rooms3D is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
 
Rooms3D is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with Rooms3D; see the file COPYING.  If not, write to 
Pixecur Technologies, 213-31 Peet Street, St. John's, NL, A1B 3W8. 
 
Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
 
*/
// Test_Win32Utils.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include <string>


#include <shlobj.h>
#include <shlwapi.h>
#include <wchar.h>
#include <shlwapi.h>

#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include <objbase.h>
#include <shlobj.h>
#include <shellapi.h>
#include <thumbcache.h>
#include <GdiPlus.h>
#include <string>
#include <iostream>

using namespace std;
typedef basic_string<WCHAR> stdwstr;

typedef void (*ExtractFileThumbnail_P)(LPCWSTR fileAbsolutePath, BOOL large, long* ,char*);
typedef BOOL (*SetProcessPriorityBelowNormal_P)();
typedef BOOL (*ExecuteShellCommand_P)(LPCWSTR command, LPCWSTR workingDirectory, LPCWSTR parameters);
typedef int  (*MapNetworkDrive_P)(LPCWSTR remPath, LPCWSTR localPath, LPCWSTR userName, LPCWSTR password, BOOL jinteractive, BOOL jprompt);
typedef int  (*DeleteNetworkDrive_P)(LPCWSTR devName, BOOL jupdProfile, BOOL jforce);
typedef int  (*GetNetworkDriveRemotePath_P)(LPCWSTR lpLocalName, char* remoteNameBuffer, int* length);

#define BUFSIZE 4096




int _tmain(int argc, _TCHAR* argv[])
{

	


 	ExtractFileThumbnail_P  ExtractFileThumbnail;
	SetProcessPriorityBelowNormal_P SetProcessPriorityBelowNormal;


    BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; 

	
	HINSTANCE hinstLib; 
	//D:\\Projects\\C\\VCPP\\dataSentinel\\Win32Utils\\Debug\\
	
	hinstLib = LoadLibrary(TEXT("Win32Utils")); 
	
	char a[25*1024];
	//perror(a);
	
	printf("hinstLib=%d;error=%u\r\n", hinstLib, GetLastError());
    if (hinstLib != NULL) 
    { 



		GetNetworkDriveRemotePath_P GetNetworkDriveRemotePath =
			(GetNetworkDriveRemotePath_P)GetProcAddress(hinstLib, "GetNetworkDriveRemotePath");

		//ExecuteShellCommand(L"http://support.pixecur.com/home/index.jsp", NULL, NULL);
		//ExecuteShellCommand(L"d:\\Temp\\upload\\video3.dvr-ms", NULL, NULL);
		//ExecuteShellCommand(L"http://msdn.microsoft.com/en-us/library/bb762154(VS.85).aspx", NULL, NULL);
		//_T ("d:\\Temp\\upload\\video.dvr-ms");
		int length = 512;
		char name[1024] = {0};
		int result = GetNetworkDriveRemotePath (L"X:", name, &length);

		printf("Result = %d; name=%s;length=%d", result, name, length);


		/*
		SetProcessPriorityBelowNormal = (SetProcessPriorityBelowNormal_P)GetProcAddress(hinstLib, "SetProcessPriorityBelowNormal"); 

		BOOL bRetValue = SetProcessPriorityBelowNormal();
		printf("Result = %d", bRetValue);


		ExtractFileThumbnail(L"C:\\Temp\\image.jpg", TRUE, &bufferSize, pngBuffer);
		
		printf("large=%d; bufferSize = %u", TRUE, bufferSize);
	
*/
	
/*
		long bufferSize = 256 * 1024;
		char* pngBuffer = new char[bufferSize];
		//printf("Before ExtractFileThumbnail\r\n");

        ExtractFileThumbnail = (ExtractFileThumbnail_P) GetProcAddress(hinstLib, "ExtractFileThumbnail"); 
		//printf("ExtractFileThumbnail=%p\r\n", ExtractFileThumbnail);
		
		//char *orig = argv[0];//"C:\\Users\\Corey\\Documents\\Data\\Pixecur\\FACT SHEET FOR dataSentinel for INTRD re Dragons.doc";


			//argv[1];

		
	 // Convert to a wchar_t*
		size_t origsize = strlen(orig) + 1;
		const size_t newsize = origsize * 2;
		size_t convertedChars = 0;
		wchar_t wcstring[newsize];
		mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE);
		*/

		/*wcscat_s(wcstring, L" (wchar_t *)");
		wcout << wcstring << endl;*/
		/*
		wchar_t *wcstring = L"https://images.scanalert.com/images/ct-header-ms.png";
			//L"C:\\Users\\Corey\\Documents\\Data\\Pixecur\\FACT SHEET FOR dataSentinel for INTRD re Dragons.doc";
		
		ExtractFileThumbnail(wcstring, TRUE, &bufferSize, pngBuffer);
		//printf("bufferSize=%d\r\n", bufferSize);
		
		HANDLE fHandle = CreateFile(L"C:\\Temp\\thumbnail.png", 
			FILE_WRITE_DATA,
			FILE_WRITE_DATA,
			NULL,
			CREATE_ALWAYS,
			FILE_ATTRIBUTE_ARCHIVE, 
			NULL);

		DWORD writtenSize = 0;
		BOOL bWriteResult = WriteFile(fHandle, pngBuffer, bufferSize, &writtenSize, NULL);
		//printf("bWriteResult=%d\r\n", bWriteResult);
		BOOL bCloseResult = CloseHandle(fHandle); 
		//printf("bCloseResult=%d\r\n", bCloseResult);
		delete pngBuffer;
		//printf("Written File ");
*/		
		/*
		//HBITMAP thumbnailImage;

		
	
		//BOOL result = ExtractFileThumbnail(_T("C:\\Temp\\7594-4386.jpg"));
		//BOOL result = ExtractFileThumbnail(_T("D:\\Temp\\crc.txt"));
		//BOOL result = ExtractFileThumbnail(_T("D:\\Temp\\test_ab.doc"));
		//BOOL result = ExtractFileThumbnail(_T("C:\\Temp\\test_ab.odt"));
		//BOOL result = ExtractFileThumbnail(_T("C:\\Temp\\stego-fs.pdf"));


		/*
		void* a =  GetProcAddress(hinstLib, "fnTest_DLL"); 



        // If the function address is valid, call the function.
 
        if (NULL != OpenFileInDefaultEditor) 
        {
            //fRunTimeLinkSuccess = TRUE;
            //(ProcAdd) (L"Message sent to the DLL function\n"); 
			OpenFileInDefaultEditor(_T("d:\\Temp\\upload\\a b\\Bear 10.wmv"));
    
		}*/
 
        // Free the DLL module.
 
        fFreeResult = FreeLibrary(hinstLib); 
	}

	/*
	CoInitialize(NULL);

	//HINSTANCE thumbExtractLibrary = CoLoadLibrary(_T("d:\\Projects\\Samples\\ThumbExtract\\ThumbExtract\\Debug\\ThumbExtract.dll"), TRUE);
	
	IExtractImage* peiURL = NULL;
	IExtractIcon* pExtractIcon = NULL;
	IShellFolder* psfDesktop = NULL;
	IShellFolder* psfWorkDir = NULL;

	basic_string<WCHAR> wsDir,wsFile,wsTempFile;

	WCHAR m_wsBuffer[1000] = {_T("d:\\Projects\\Documentation\\dataSentinel\\")};
	//{_T("c:\\temp\\")};
	//{_T("C:\\Users\\Public\\Pictures\\Sample Pictures\\")};
	
	WCHAR fileNameWS[1000] = 
	//{_T("ih99-stegfs.pdf")};
	//{_T("Winter.jpg")};
	{_T("PxServices Refactoring.docx")};

	
	
	LPITEMIDLIST pidlItems = NULL, pidlURL = NULL, pidlWorkDir = NULL;


	//wcscpy(m_wsBuffer,wsDir.c_str());


	HRESULT hr = SHGetDesktopFolder(&psfDesktop);
	
	
	hr = psfDesktop->ParseDisplayName(NULL, NULL, m_wsBuffer, NULL, &pidlWorkDir, NULL);
	
	if(hr != S_OK)
		cout <<"Failed 1"<<endl;

	hr = psfDesktop->BindToObject(pidlWorkDir, NULL, IID_IShellFolder, (LPVOID*)&psfWorkDir);

	if(hr != S_OK)
		cout <<"Failed 2"<<endl;



	//{_T("devlist.txt")}; 

    hr = psfWorkDir->ParseDisplayName(NULL, NULL, fileNameWS, NULL, &pidlURL, NULL);
   
   // query IExtractImage
    hr = psfWorkDir->GetUIObjectOf(NULL, 1, (LPCITEMIDLIST*)&pidlURL, IID_IExtractImage, NULL, (LPVOID*)&peiURL);
	
	if(hr == S_OK)
		cout<<"Found Extract Image Iface"<<endl;

    hr = psfWorkDir->GetUIObjectOf(NULL, 1, (LPCITEMIDLIST*)&pidlURL, IID_IExtractIcon, NULL, (LPVOID*)&pExtractIcon);
	
	if(hr == S_OK)
		cout<<"Found Extract Icon Iface"<<endl;


	


	





	HBITMAP hBitmap;

//	hr = pExtractIcon->Extract(&hBitmap);



	ULONG_PTR g_startupToken;           // for starting GdiPlus
	HBITMAP g_hThumbnail;				// Thumbnail to create
	WTS_ALPHATYPE g_alphaType;			// Indicates whether thumbnail has alpha
	int g_nThumbSize = 0;				// Size of thumbnail


    //PCWSTR pszArg1 = L"d:\\Books\\FileSystem\\ih99-stegfs.pdf";
		//L"C:\\Users\\Public\\Pictures\\Sample Pictures\\Creek.jpg"; // ppszArgs[0]; 
    //PCWSTR pszArg2 = ppszArgs[1];


    g_nThumbSize = 16;//_wtoi(pszArg2);

    ///hr = CoInitialize(NULL);
*/
	/*
    if (SUCCEEDED(hr))
    {
        IShellItem* pFileItem;
        //hr = SHCreateItemFromParsingName(pszArg1, NULL, IID_PPV_ARGS(&pFileItem));

		//IShellItem *pShellItem1 = NULL;
///IShellItem *pShellItem2 = NULL;

		hr = ::SHCreateShellItem(NULL, psfWorkDir, (LPCITEMIDLIST)pidlURL, &pFileItem);

        if (SUCCEEDED(hr))
        {
            IThumbnailProvider *pThumbProvider;
            hr = pFileItem->BindToHandler(NULL, BHID_ThumbnailHandler, IID_PPV_ARGS(&pThumbProvider));
            
			if(hr == S_OK)
				cout<<"Found Thumbnail Handler Iface"<<endl;


			if (SUCCEEDED(hr))
            {
                hr = pThumbProvider->GetThumbnail(g_nThumbSize, &g_hThumbnail, &g_alphaType);


                pThumbProvider->Release();
            }
            pFileItem->Release();
        }
	}






	CoUninitialize();

	*/

	char c;
	cin >> c;


	return 0;
}

